home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / BORLAND TURBO / TUTORIAL.PAK / CPPOCF1.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  8.0 KB  |  369 lines

  1. // ---------------------------------------------------------------------------
  2. // Copyright (C) 1994 Borland International
  3. // CppOcf1.cpp
  4. // ---------------------------------------------------------------------------
  5. #include <ocf/pch.h>
  6. #include <ocf/ocapp.h>
  7. #include <windowsx.h>
  8. #include <ocf/ocdoc.h>
  9. #include <ocf/ocview.h>
  10. #include <ocf/ocpart.h>
  11. #include "ocfevx.h"
  12. #include "CppOcf1.h"
  13.  
  14. //
  15. // OCF variables that are needed per window
  16. //
  17. TOcRegistrar* OcRegistrar = 0;
  18. TOcApp*       OcApp       = 0;
  19. TOcDocument*  OcDoc       = 0;
  20. TOcView*      OcView      = 0;
  21.  
  22. bool Inserted = false;
  23. HWND HwndView = 0;
  24. TRegLink* RegLinkHead = 0;  // initialize reglink list
  25.  
  26. //
  27. // registration information
  28. //
  29. REGISTRATION_FORMAT_BUFFER(100)
  30.  
  31. BEGIN_REGISTRATION(AppReg)
  32.   REGDATA(clsid,    "{8646DB80-94E5-101B-B01F-00608CC04F66}")
  33. END_REGISTRATION
  34.  
  35. //
  36. // Starting point of all Windows programs
  37. //
  38. int PASCAL
  39. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  40.         char far* lpCmdLine, int nCmdShow)
  41. {
  42.   try {
  43.     TOleAllocator allocator(0);  // Required for OLE2
  44.     MSG msg;
  45.  
  46.     // Initialize OCF objects
  47.     //
  48.     string cmdLine(lpCmdLine);
  49.     OcRegistrar = new TOcRegistrar(::AppReg, 0, cmdLine,
  50.                                    ::RegLinkHead, hInstance);
  51.     OcRegistrar->CreateOcApp(OcRegistrar->GetOptions(), OcApp);
  52.  
  53.     // Any previous instance?
  54.     if (!hPrevInstance)
  55.       // Initialize app-specific stuff
  56.       if (!InitApplication(hInstance))
  57.         return 0;
  58.  
  59.     // Instance-specific
  60.     if (!InitInstance(hInstance, nCmdShow))
  61.       return 0;
  62.  
  63.     // Standard Windows message loop
  64.     while (GetMessage(&msg, 0, 0, 0)) {
  65.       TranslateMessage(&msg);
  66.       DispatchMessage(&msg);
  67.     }
  68.  
  69.   }
  70.   catch (TXBase& xbase) {
  71.     MessageBox(GetFocus(), xbase.why().c_str(), "Exception caught", MB_OK);
  72.   }
  73.  
  74.   // free the OCF object
  75.   //
  76.   delete OcRegistrar;
  77.   return 0;
  78. }
  79.  
  80.  
  81. //
  82. // Initialize application-specific stuff and register the class
  83. //
  84. bool
  85. InitApplication(HINSTANCE hInstance)
  86. {
  87.   WNDCLASS wc;
  88.   WNDCLASS wcView;
  89.  
  90.   wc.style          = 0;
  91.   wc.lpfnWndProc    = (WNDPROC) MainWndProc;
  92.   wc.cbClsExtra     = 0;
  93.   wc.cbWndExtra     = 0;
  94.   wc.hInstance      = hInstance;
  95.   wc.hIcon          = LoadIcon(0, IDI_APPLICATION);
  96.   wc.hCursor        = LoadCursor(0, IDC_ARROW);
  97.   wc.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
  98.   wc.lpszMenuName   = MENUNAME;
  99.   wc.lpszClassName  = CLASSNAME;
  100.  
  101.   RegisterClass(&wc);
  102.  
  103.   wcView.style          = 0;
  104.   wcView.lpfnWndProc    = (WNDPROC) ViewWndProc;
  105.   wcView.cbClsExtra     = 0;
  106.   wcView.cbWndExtra     = 0;
  107.   wcView.hInstance      = hInstance;
  108.   wcView.hIcon          = LoadIcon(0, IDI_APPLICATION);
  109.   wcView.hCursor        = LoadCursor(0, IDC_ARROW);
  110.   wcView.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
  111.   wcView.lpszMenuName   = 0;
  112.   wcView.lpszClassName  = VIEWCLASSNAME;
  113.  
  114.   RegisterClass(&wcView);
  115.  
  116.   return true;
  117. }
  118.  
  119. //
  120. // Initialize instance and display the main window
  121. //
  122. bool
  123. InitInstance(HINSTANCE hInstance, int nCmdShow)
  124. {
  125.   HWND hwnd;
  126.  
  127.   hwnd = CreateWindow(CLASSNAME, TITLE,
  128.     WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  129.     CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  130.     0, 0, hInstance, 0);
  131.  
  132.   if (!hwnd)
  133.     return false;
  134.   ShowWindow(hwnd, nCmdShow);
  135.   UpdateWindow(hwnd);
  136.   return true;
  137. }
  138.  
  139.  
  140. //
  141. // Standard message-handler routine for main window
  142. //
  143. long CALLBACK _export
  144. MainWndProc(HWND hwnd, uint message, WPARAM wParam, LPARAM lParam)
  145. {
  146.   switch (message) {
  147.     HANDLE_MSG(hwnd, WM_CREATE,   MainWnd_OnCreate);
  148.     HANDLE_MSG(hwnd, WM_CLOSE,    MainWnd_OnClose);
  149.     HANDLE_MSG(hwnd, WM_DESTROY,  MainWnd_OnDestroy);
  150.     HANDLE_MSG(hwnd, WM_COMMAND,  MainWnd_OnCommand);
  151.     HANDLE_MSG(hwnd, WM_SIZE,     MainWnd_OnSize);
  152.  
  153.     // handle the OCF to application messages
  154.     //
  155.     HANDLE_MSG(hwnd, WM_OCEVENT,  MainWnd_OnOcEvent);
  156.   }
  157.   return DefWindowProc(hwnd, message, wParam, lParam);
  158. }
  159.  
  160. bool
  161. MainWnd_OnCreate(HWND hwnd, CREATESTRUCT FAR* /*lpCreateStruct*/)
  162. {
  163.   if (OcApp)
  164.     OcApp->SetupWindow(hwnd);
  165.  
  166.   HwndView = CreateWindow(VIEWCLASSNAME, "",
  167.     WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE | WS_BORDER,
  168.     10, 10, 300, 300,
  169.     hwnd, (HMENU)1, _hInstance, 0);
  170.  
  171.   return true;
  172. }
  173.  
  174. void
  175. MainWnd_OnClose(HWND hwnd)
  176. {
  177.   if (IsWindow(HwndView))
  178.     DestroyWindow(HwndView);
  179.   DestroyWindow(hwnd);
  180. }
  181.  
  182. void
  183. MainWnd_OnDestroy(HWND /*hwnd*/)
  184. {
  185.  
  186.   PostQuitMessage(0);
  187. }
  188.  
  189. void
  190. MainWnd_OnCommand(HWND hwnd, int id, HWND /*hwndCtl*/, uint /*codeNotify*/)
  191. {
  192.   switch (id) {
  193.     case CM_INSERTOBJECT: {
  194.       try {
  195.         TOcInitInfo initInfo(OcView);
  196.  
  197.         if (OcApp->Browse(initInfo)) {
  198.           TRect rect(30, 30, 100, 100);
  199.           new TOcPart(*OcDoc, initInfo, rect);
  200.           Inserted = true;
  201.         }
  202.       }
  203.       catch (TXBase& xbase) {
  204.         MessageBox(GetFocus(), xbase.why().c_str(), "Exception caught", MB_OK);
  205.       }
  206.       break;
  207.     }
  208.     case CM_EXIT: {
  209.       PostMessage(hwnd, WM_CLOSE, 0, 0);
  210.       break;
  211.     }
  212.   }
  213. }
  214.  
  215. void
  216. MainWnd_OnSize(HWND hwnd, UINT /*state*/, int /*cx*/, int /*cy*/)
  217. {
  218.   if (IsWindow(HwndView)) {
  219.     TRect rect;
  220.     GetClientRect(hwnd, &rect);
  221.     MoveWindow(HwndView, rect.left, rect.top, rect.right, rect.bottom, true);
  222.   }
  223. }
  224.  
  225. bool
  226. MainWnd_OnOcAppFrameRect(HWND hwnd, TRect far* rect)
  227. {
  228.   GetClientRect(hwnd, rect);
  229.   return true;
  230. }
  231.  
  232. //
  233. // Subdispatch OC_... messages
  234. //
  235. long
  236. MainWnd_OnOcEvent(HWND hwnd, WPARAM wParam, LPARAM lParam)
  237. {
  238.   switch (wParam) {
  239.     HANDLE_OCF(hwnd, OC_APPFRAMERECT, MainWnd_OnOcAppFrameRect);
  240.   }
  241.   return false;
  242. }
  243.  
  244. //
  245. // view window
  246. //
  247. bool
  248. ViewWnd_OnCreate(HWND hwnd, CREATESTRUCT FAR* /*lpCreateStruct*/)
  249. {
  250.   OcDoc  = new TOcDocument(*OcApp);
  251.   OcView = new TOcView(*OcDoc);
  252.  
  253.   if (OcView)
  254.     OcView->SetupWindow(hwnd);
  255.  
  256.   return true;
  257. }
  258.  
  259. void
  260. ViewWnd_OnClose(HWND hwnd)
  261. {
  262.   PostMessage(GetParent(hwnd), WM_CLOSE, 0, 0);
  263.   DestroyWindow(hwnd);
  264. }
  265.  
  266. void
  267. ViewWnd_OnDestroy(HWND /*hwnd*/)
  268. {
  269.   for (TOcPartCollectionIter i(OcDoc->GetParts()); i; i++) {
  270.     TOcPart& p = *i.Current();
  271.     p.Activate(false);
  272.   }
  273.  
  274.   if (OcView) {
  275.     OcView->EvClose();
  276.     OcView->ReleaseObject();
  277.   }
  278.  
  279.   OcDoc->Close();
  280.  
  281.   if (OcApp) {
  282.     OcApp->Release();
  283.   }
  284.  
  285.   delete OcDoc;
  286. }
  287.  
  288. void
  289. ViewWnd_OnPaint(HWND hwnd)
  290. {
  291.   PAINTSTRUCT ps;
  292.   HDC dc = BeginPaint(hwnd, &ps);
  293.  
  294.   RECT rect;
  295.   GetClientRect(hwnd, &rect);
  296.  
  297.   TRect clientRect(rect);
  298.   TRect logicalRect = clientRect;
  299.   DPtoLP(dc, (POINT*)&logicalRect, 2);
  300.  
  301.   for (TOcPartCollectionIter i(OcDoc->GetParts()); i; i++) {
  302.     TOcPart& p = *i.Current();
  303.     if (p.IsVisible(logicalRect)) {
  304.       TRect r = p.GetRect();
  305.       p.Draw(dc, r, clientRect, asDefault);
  306.     }
  307.   }
  308.  
  309.   EndPaint(hwnd, &ps);
  310. }
  311.  
  312. void
  313. ViewWnd_OnSize(HWND /*hwnd*/, UINT /*state*/, int /*cx*/, int /*cy*/)
  314. {
  315.   if (OcView) {
  316.     OcView->EvResize();
  317.   }
  318. }
  319.  
  320. //
  321. // Standard message-handler routine for view window
  322. //
  323. long CALLBACK _export
  324. ViewWndProc(HWND hwnd, uint message, WPARAM wParam, LPARAM lParam)
  325. {
  326.   switch (message) {
  327.     HANDLE_MSG(hwnd, WM_CREATE,   ViewWnd_OnCreate);
  328.     HANDLE_MSG(hwnd, WM_CLOSE,    ViewWnd_OnClose);
  329.     HANDLE_MSG(hwnd, WM_DESTROY,  ViewWnd_OnDestroy);
  330.     HANDLE_MSG(hwnd, WM_PAINT,    ViewWnd_OnPaint);
  331.  
  332.     // handle the OCF to application messages
  333.     //
  334.     HANDLE_MSG(hwnd, WM_OCEVENT,  ViewWnd_OnOcEvent);
  335.   }
  336.   return DefWindowProc(hwnd, message, wParam, lParam);
  337. }
  338.  
  339.  
  340. const char*
  341. ViewWnd_OnOcViewTitle(HWND /*hwnd*/)
  342. {
  343.   return APPSTRING;
  344. }
  345.  
  346.  
  347. bool
  348. ViewWnd_OnOcViewClose(HWND /*hwnd*/)
  349. {
  350.   if (OcDoc)
  351.     OcDoc->Close();
  352.   return true;
  353. }
  354.  
  355. //
  356. // Subdispatch OC_... messages
  357. //
  358. long
  359. ViewWnd_OnOcEvent(HWND hwnd, WPARAM wParam, LPARAM /*lParam*/)
  360. {
  361.   switch (wParam) {
  362.     HANDLE_OCF(hwnd, OC_VIEWCLOSE,  ViewWnd_OnOcViewClose);
  363.     HANDLE_OCF(hwnd, OC_VIEWTITLE,  ViewWnd_OnOcViewTitle);
  364.   }
  365.   return false;
  366. }
  367.  
  368.  
  369.